#!/bin/bash

apiHost="http://58.240.254.106:4080"
softVersion="20170405"
accountSid="304d57983ada9f4b287357a8de3c15dc"
authToken="f43708b004f2797c0b6f43b087ef940a"
appId="bfb94fd651dbea92f4bb0837e328b3ee"
subAccountSid="c6237c2c70b3eb9be43bf7b479cc9439"
subAccountToken="43e2eb615f68e8c2a8ac2689f018efde"
dataFormatJson=0

function InitValue()
{
    timeFormat=`date +%Y%m%d%H%M%S`;
    accountSig=`echo -n ${accountSid}${authToken}${timeFormat} | md5sum | awk '{print toupper($1)}'`;
    accountAuth=`echo -n ${accountSid}:${timeFormat} | base64`;
    subAccountSig=`echo -n $subAccountSid$subAccountToken$timeFormat | md5sum | awk '{print toupper($1)}'`;
    subAccountAuth=`echo -n $subAccountSid:$timeFormat | base64`;
    echo "subAccountAuth:$subAccountAuth"
    if [ "$1" = "$dataFormatJson" ]; then
        contentType="Content-Type:application/json;charset=utf-8;";
        accept="Accept:application/json";
    else
        contentType="Content-Type:application/xml;charset=utf-8;";
        accept="Accept:application/xml";
    fi
    accountAuthorization="Authorization:${accountAuth}";
    subAuthorization="Authorization:${subAccountAuth}";
    return 0;
}

#$1: workNumber $2: deviceNumber $3: deviceType
function __changeCallType(){
    _Change_Call_Type_URL="$apiHost/$softVersion/SubAccounts/$subAccountSid/CallCenter/__changeCalltype?sig=$subAccountSig";
    data=`jq -n -c -M --arg x $appId --arg workNumber $1 --arg deviceNumber $2 --arg deviceType $3 '{"__changeCalltype": {"appId":$x,"workNumber":$workNumber, "deviceNumber":$deviceNumber, "type":$deviceType}}'`;
    _Change_Call_Type_RespData=`curl -d $data -X POST --connect-timeout 10 -H $accept -H $contentType -H $subAuthorization $_Change_Call_Type_URL`;
    echo $data
    echo $_Change_Call_Type_RespData;
    return 0;
}


InitValue 0
OLD_IFS="$IFS"
header=0

while read line
do
    if [ $header = 0 ];then
        header=1
    else
        IFS=","
        arr=($line)
        IFS=$OLD_IFS
        __changeCallType "${arr[0]}" "${arr[1]}" "${arr[2]}"
    fi
done < name.csv
IFS="$OLD_IFS"
exit 0
